package com.csthink.web.interfaces;

import com.csthink.common.infrastructure.enums.ErrorEnum;
import com.csthink.common.infrastructure.exception.BaseException;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-10
 */
@Api(tags = "Test 模块")
@RestController
@RequestMapping(value = "/session")
public class SessionTestController {

    @GetMapping("/login")
    public String login(@Validated @NotEmpty @RequestParam String username, @RequestParam String password, HttpSession session) {
        session.setAttribute("login_user", username);
        return "登录成功";
    }

    @GetMapping("/login-out")
    public String loginOut(HttpSession session) {
        session.removeAttribute("login_user");
        return "登出成功";
    }

    @GetMapping("/api")
    public String api(HttpSession session) {
        String loginUser = (String) session.getAttribute("login_user");

        if (StringUtils.isEmpty(loginUser)) {
            throw new BaseException(ErrorEnum.USER_NOT_LOGIN);
        }

        return "查询成功";
    }
}
